package com.eurabia.eggchat.utils;




import org.apache.commons.codec.binary.Hex;
import org.springframework.util.DigestUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * 功能描述：加密工具
 * Created by zhanghuijiang
 * on 2018/5/24.
 */
public class EncryptionUtil {

    /**
     * base64 编码
     * @param password 未编码密码
     * @return
     */
    public static String encryptionBase64(String password)
    {
        //加密后字符串
        return new BASE64Encoder().encode(password.getBytes());
    }

    /**
     * base64 解码
     * @param passwordEn 编码后密码
     * @return
     */
    public static String decryptionBase64(String passwordEn)
    {
        BASE64Decoder base64Decoder = new BASE64Decoder();
        try {
            new String(base64Decoder.decodeBuffer(passwordEn),"UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * MD5加密
     * @param password
     * @return
     */
    public static String encryptionMD5(String password)
    {
        return DigestUtils.md5DigestAsHex(password.getBytes());
    }


    /**
     * MD5 +加盐 加密
     * @param password
     * @param salt
     * @return
     */
    public static String encryptionMD5AndSalt(String password,String salt)
    {
        try {
            StringBuffer stingBuffer = new StringBuffer();
            // 1.指定加密算法
            MessageDigest digest = MessageDigest.getInstance("MD5");
            // 2.将需要加密的字符串转化成byte类型的数据，然后进行哈希过程
            byte[] bs = digest.digest((password + salt).getBytes());
            // 3.遍历bs,让其生成32位字符串，固定写法
            // 4.拼接字符串
            for (byte b : bs) {
                int i = b & 0xff;
                String hexString = Integer.toHexString(i);
                if (hexString.length() < 2) {
                    hexString = "0" + hexString;
                }
                stingBuffer.append(hexString);
            }
            return stingBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * SHA256加密
     * @param password
     * @return
     */
    public static String encryptionSHA256(String password)
    {
        String passwordEn = org.apache.commons.codec.digest.DigestUtils.sha256Hex(password);
        return passwordEn;
    }

    /**
     * SHA256加密+加盐
     * @param password
     * @return
     */
    public static String encryptionSHA256AndSalt(String password)
    {
        String salt = UUID.randomUUID().toString();
        MessageDigest messageDigest =org.apache.commons.codec.digest.DigestUtils.getSha256Digest();
        return returnPassword(messageDigest,password,salt);
    }

    /**
     *
     * 加密 SHA256加密+加盐
     *
     * 密文:加密算法$随机数$加密(加密算法$随机数$明文)
     * @param password 明文
     * @return 密文
     */
    public static String encryptionSHA256AndRandomAndSalt(String password)
    {
        RegexUtil regexUtil = new RegexUtil();
		/* 加密 */
        String encType = "sha256";
        String randStr = regexUtil.proccedStr(32) ;
		/* 加密前置字符串 (sha256$abcd$)*/
        String encStr = encType + "$" + randStr + "$";
		/* 加密前串 sha256$abcd$11111*/
        String encHead = encStr + password;
		/* 明文加密 */
        String encBack = org.apache.commons.codec.digest.DigestUtils.sha256Hex(encHead);
		/* sha256$abcd$sha256(sha256$abcd$11111) */
        return encStr + encBack ;

    }

    /**
     * 校验密码
     * @param souPass
     * @param encPass
     * @return
     */
    public static boolean checkPass(String souPass,String encPass)
    {
        String [] encPassEs = encPass.split("\\$");
        //加密前置字符串 (sha256$abcd$)
        String encStr = encPassEs[0] + "$" + encPassEs[1] + "$";
        /* 加密前串 sha256$abcd$11111*/
        String encHead = encStr + souPass;
        //明文加密
        String encBack = org.apache.commons.codec.digest.DigestUtils.sha256Hex(encHead);
        encBack = encStr + encBack;
        return encBack.equals(encPass);
    }

    /**
     * SHA512加密
     * @param password
     * @return
     */
    public static String encryptionSHA512(String password)
    {
        String passwordEn = org.apache.commons.codec.digest.DigestUtils.sha512Hex(password);
        return passwordEn;
    }

    /**
     * SHA512加密+加盐
     * @param password
     * @return
     */
    public static String encryptionSHA512AndSalt(String password)
    {
        String salt = UUID.randomUUID().toString();
        MessageDigest messageDigest =org.apache.commons.codec.digest.DigestUtils.getSha512Digest();
        return returnPassword(messageDigest,password,salt);
    }

    /**
     * 返回加密后密码
     * @param messageDigest
     * @param password
     * @param salt
     * @return
     */
    private static String returnPassword(MessageDigest messageDigest,String password,String salt)
    {
        messageDigest.update(salt.getBytes());
        byte[] psEn = messageDigest.digest(password.getBytes());
        for (int i = 0; i < 9 ; i++)
        {
            messageDigest.reset();
            psEn = messageDigest.digest(psEn);
        }
        String passwordEn = Hex.encodeHexString(psEn);
        return passwordEn;
    }

    /**
     * 测试main方法
     * @param args
     */
    public static void main(String[] args) {
        String souPass = "test1";
        String result = EncryptionUtil.encryptionSHA256AndRandomAndSalt(souPass);
        System.out.println("mySalt:" + result);

        boolean flag = EncryptionUtil.checkPass("test1",result);
        System.out.println("校验结果："+ flag);

    }
}
